package com.gm.wholesale.bluetooth.weightBlue;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.gm.commonlib.util.stringUtils.StringUtils;
import com.gm.wholesale.bluetooth.BluetoothUtil;
import com.gm.wholesale.bluetooth.bean.WeightBean;
import com.gm.wholesale.bluetooth.service.BluetoothService;
import com.youth.banner.util.LogUtils;

public class WXLWeightBluetooth extends BaseWeightBluetooth {

    private static WXLWeightBluetooth mWXLWeightBluetooth;

    public static WXLWeightBluetooth getBluetoothManger(Context context) {
        if (mWXLWeightBluetooth == null) {
            mWXLWeightBluetooth = new WXLWeightBluetooth();
            mBluetoothUtil = BluetoothUtil.newInstance();
            mWXLWeightBluetooth.initWeightBluetoothManger(context);
        }
        return mWXLWeightBluetooth;
    }

    public WXLWeightBluetooth initWeightBluetoothManger(Context context) {

        mContext = context;
        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 100:
                        byte[] readBuf = (byte[]) msg.obj;

                        WeightBean weightBean=getWeightBean(readBuf,msg.arg1);
                        if (weightBean!=null) {
                            if ( mListener!= null) {
                                mListener.getNumber(weightBean);
                            }
                        }
                        break;
                    case 2:
                        if (msg.arg1 < 2) {
                            LogUtils.d("连接失败");
                            if (mListener != null) {
                                mListener.connectionFail();
                            }
                        } else {
                            if (mListener != null && mBluetoothService!=null) {
                                mListener.connection(mBluetoothService.getConnectionDevice(context));
                               // byte sendData[]= HexUtil.getStringhex("QZ1170JS");
                               // write(sendData);
                            }

                        }
                }
            }
        };

        mBluetoothService = new BluetoothService(mContext, mHandler);
        mBluetoothService.setIonHandelDataListener((readByteSize, buffer, handler) -> {

            byte[] buffer2 = new byte[1024];
            int i;
            if (readByteSize > 0) {
                buffer2 = buffer.clone();
                for (i = 0; i < buffer.length; i++) buffer[i] = 0;
                Message message=Message.obtain();
                message.obj=buffer2;
                message.what=100;
                message.arg1=readByteSize;
                mHandler.sendMessage(message);

            }
        });
        return mWXLWeightBluetooth;
    }

    WeightBean getWeightBean(byte[] dat, int len) {
        try {
            String data=new String(dat, 0, len, "GBK");
            LogUtils.d("接受到数据: "+data);
            WeightBean weightBean=new WeightBean();
           if (data!=null){
               String numbers[]=data.split(" ");
               for (String num:numbers){
                   LogUtils.d("数据: "+num);
               }
               weightBean.setWeight(numbers[3]);
               weightBean.setsUnit(StringUtils.getEnglishWord(data));
           }
            LogUtils.d("接受到数据处理 : "+data);
            LogUtils.d("转换后的数据 : "+weightBean.toString());
            return weightBean;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public void stop() {
        mWXLWeightBluetooth = null;
        mHandler = null;
        mBluetoothService.stop();
        mBluetoothService = null;
    }


}
